import time

from django.db import models

DEFAULT_NAME = "魅影陌客"
DEFAULT_DETAIL = "Phantom Stranger"
DEFAULT_AVATAR = "oracle/images/avatar/default.jpg"


class Status(models.IntegerChoices):
    active = 0, "ACTIVE"
    inactive = 1, "INACTIVE"
    frozen = 2, "FROZEN"
    obsolete = 3, "OBSOLETE"


class BaseMeta:
    ordering = ['level', 'status', 'id']


class BaseModel(models.Model):
    """
    基础model, 提供通用的属性字段
    """
    # 索引key
    key = models.CharField("查找索引", max_length=64, unique=True, blank=True, null=True)
    # 节点状态
    status = models.IntegerField("状态信息", choices=Status.choices, default=0)
    # 排序等级
    level = models.BigIntegerField("权重", default=time.time_ns())
    # 创建修改时间
    create_time = models.TimeField("创建时间", auto_now_add=True)
    modify_time = models.TimeField("修改时间", auto_now=True)

    # 数据显示名称
    def __str__(self):
        if hasattr(self, 'name'):
            return self.name
        if hasattr(self, 'title'):
            return self.title
        return self.key

    # 获取app名称， gideon主动批量添加url使用
    @classmethod
    def get_app_label(cls, alias=True):
        if alias and hasattr(cls, 'custom_app_label'):
            return cls.custom_app_label
        else:
            return cls._meta.app_label

    # 获取model名称， gideon主动批量添加url使用
    @classmethod
    def get_model_name(cls, alias=True):
        if alias and hasattr(cls, 'custom_model_name'):
            return cls.custom_model_name
        else:
            return cls._meta.model_name

    # 获取verbose name
    @classmethod
    def get_verbose_name(cls, key):
        return cls._meta.get_field(key).verbose_name

    @classmethod
    def get_key_show_name(cls):
        return cls.key.field.verbose_name

    @classmethod
    def get_status_show_name(cls):
        return cls.status.field.verbose_name

    @classmethod
    def get_level_show_name(cls):
        return cls.level.field.verbose_name

    class Meta:
        # 设置此类为继承类，数据迁移时不生成表
        abstract = True
